package com.lft.com.oilgas.service.impl;

import com.lft.com.oilgas.entity.Accounts;
import com.lft.com.oilgas.entity.Merchant;
import com.lft.com.oilgas.mapper.AccountsMapper;
import com.lft.com.oilgas.mapper.MerchantMapper;
import com.lft.com.oilgas.service.IAccountsSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountsSericeImpl implements IAccountsSerice {
    @Autowired
    private AccountsMapper accountsMapper;
    @Autowired
    private MerchantMapper merchantMapper;


    @Override
    public int insertOrUpdateacc(Accounts accounts) {
        if (accounts.getId() != null){
          return accountsMapper.updateById(accounts);
        }else {
          return accountsMapper.insert(accounts);
        }
    }

    @Override
    public void insertOrUpdate(Accounts accounts) {
        if (accounts.getId() != null && accounts.getId() != 0) {
            accountsMapper.updateById(accounts);
        } else {
            accountsMapper.insert(accounts);
            if (accounts != null && accounts.getId() != null && accounts.getId() != 1) {
                Merchant merchant = merchantMapper.selectById(accounts.getMerchantid());
                String id = String.valueOf(accounts.getId());
                merchant.setOpenanAccount(id);
                merchantMapper.updateById(merchant);
            }
        }
    }
    @Override
    public Accounts selectByid(Integer id) {
        return  accountsMapper.selectById(id);
    }
}
